package com.chinairi.mall.controller;

import com.chinairi.common.utils.IdGenerator;
import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.*;
import com.chinairi.mall.service.IOrderGoodsService;
import com.chinairi.mall.service.IOrderInfoService;
import com.chinairi.mall.service.IPurchasePlanService;
import com.chinairi.mall.util.PurchaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yyp
 * @date 2020/9/29 15:55
 */
@RestController
@RequestMapping("/PurchasePlan")
public class PurchasePlanController {
    @Autowired
    private IOrderGoodsService orderGoodsService;
    @Autowired
    private IOrderInfoService orderInfoService;

    private static final long serialVersionUID = 4241666445122360670L;

    @Autowired
    private IPurchasePlanService iPurchasePlanService;

    /**
     * 物质采购订单入库信息获取
     *
     * @param purInStorage
     * @return
     */
    @GetMapping("/purchaseInStorage")
    public Map<String, Object> purchaseInStorage(@RequestBody PurInStorage purInStorage) {
        return iPurchasePlanService.savePurchaseInStorage(purInStorage);
    }

    /**
     * 物质采购订单出库信息获取
     *
     * @return
     */
    @GetMapping("/purchaseOutStorage")
    public Map<String, Object> purchaseOutStorage(@RequestBody PurOutStorage purOutStorage) {
        return iPurchasePlanService.savePurchaseOutStorage(purOutStorage);
    }

    /**
     * 现场验收签收数据获取
     *
     * @return
     */
    @GetMapping("/signInData")
    public Map<String, Object> signInData(@RequestBody SignData signData) {
        return iPurchasePlanService.saveSignInData(signData);
    }


    /**
     * 结算数据获取
     *
     * @return
     */
    @GetMapping("/accountData")
    public Map<String, Object> accountData(AccountData accountData) {
        Map<String, Object> map = new HashMap<>();
        DecimalFormat df = new DecimalFormat("#0.00");
        double actualRe = (new Random().nextDouble()) * (accountData.getReceivable());
        double actualPay = (new Random().nextDouble()) * (accountData.getPayable());
        String actualReceivable = df.format(actualRe);
        String actualPayable = df.format(actualPay);
        OrderGoodsInfoParam orderInfoParam = new OrderGoodsInfoParam();
        orderInfoParam.setOrderNo(accountData.getOrderNo());
        orderInfoParam.setReceivable(BigDecimal.valueOf(accountData.getReceivable()));
        orderInfoParam.setPayAble(BigDecimal.valueOf(accountData.getPayable()));
        String ogId = IdGenerator.UUID();
        OrderGoodsInfoParam orderGoodsParam = new OrderGoodsInfoParam();
        orderGoodsParam.setOrderNo(accountData.getOrderNo());
        orderGoodsParam.setReceivable(BigDecimal.valueOf(accountData.getReceivable()));
        orderGoodsParam.setPayAble(BigDecimal.valueOf(accountData.getPayable()));
        orderGoodsParam.setOgId(ogId);
        try {
            orderGoodsService.saveOrderGoods(orderGoodsParam);
            orderInfoService.saveOrderInfo(orderInfoParam);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败", false);
        }
        map.put("orderNo", accountData.getOrderNo());
        map.put("receivable", accountData.getReceivable());
        map.put("payable", accountData.getPayable());
        map.put("actualReceivable", actualReceivable);
        map.put("actualPayable", actualPayable);
        return map;
    }


}
